using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Wax.Runner;

namespace Wax.ConsoleRunner
{
    internal class ConsoleRunner
    {
        #region Private fields

        /// <summary>
        /// Filename to the test script file
        /// </summary>
        private string fileName;

        /// <summary>
        /// <c>true</c> if any errors occured during test execution.
        /// </summary>
        private bool errorsOccured;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleRunner"/> class.
        /// </summary>
        /// <param name="fileName">Name of the test script file.</param>
        public ConsoleRunner(string fileName)
        {
            this.FileName = fileName;
        }

        #endregion

        #region Public instance methods

        public void Run()
        {
            this.errorsOccured = false;
            TestRunner testRunner = new TestRunner(this.fileName);
            
            testRunner.TestCompleted += new EventHandler(TestRunnerTestCompleted);
            testRunner.ExecutionCompleted += new EventHandler(TestRunnerExecutionCompleted);

            testRunner.Load();
            testRunner.Execute();
        }

        /// <summary>
        /// Handles the TestCompleted event of the testRunner control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TestRunnerTestCompleted(object sender, EventArgs e)
        {
            Test test = sender as Test;

            if (test != null)
            {               
                if (test.Errors.Count > 0)
                {
                    Program.WriteError(string.Format(CultureInfo.InvariantCulture, Resource.TestCompletedMessageFail, test.Id));
                }
                else
                {
                    Program.WriteSuccess(string.Format(CultureInfo.InvariantCulture, Resource.TestCompletedMessageSuccess, test.Id));
                }
            }
        }

        /// <summary>
        /// Handles the ExecutionCompleted event of the testRunner control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TestRunnerExecutionCompleted(object sender, EventArgs e)
        {
            TestRunner testRunner = sender as TestRunner;

            if (testRunner != null)
            {
                if (testRunner.Errors.Count > 0)
                {
                    this.errorsOccured = true;
                    StringBuilder errorMessage = new StringBuilder();
                    foreach (TestError error in testRunner.Errors)
                    {
                        errorMessage.Append(error.ToString() + System.Environment.NewLine);
                    }

                    Program.WriteError(string.Format(CultureInfo.InvariantCulture , Resource.TestExecutionCompletedFail, errorMessage.ToString()));
                }
                else
                {
                    Program.WriteSuccess(Resource.TestExecutionCompletedSuccess);
                }
            }
        }

        #endregion

        #region Public properties

        /// <summary>
        /// Filename to the test script file
        /// </summary>
        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public bool ErrorsOccured
        {
            get
            {
                return this.errorsOccured;
            }
        }

        #endregion

    }
}
